package lab03.test07;

import org.omg.CORBA.portable.OutputStream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Test07 {

    public static void main(String[] args) {

        /*
         * 读取book.zip,并将book.zip中含有的文件重新存放到当前目录中的book文件夹中
         * 即将book.zip的内容解压到book文件夹中
         * */

        String path1 = "D:TestZip.zip";
        String path2 = "D:TestZip";


        ZipInputStream zipInputStream = null;
        ZipFile zipFile = null;

        //创建一个zip文件对象
        try {
            zipFile = new ZipFile(path1);
            Enumeration entries = zipFile.entries();
            ZipEntry zipEtry = null;

            while (entries.hasMoreElements()){
                zipEtry = (ZipEntry) entries.nextElement();
                System.out.println(zipEtry.getName());

                File testFile = new File(path2);

                //创建文件夹
                testFile.mkdir();

                //字节流 读文件 写文件
                InputStream inputStream = zipFile.getInputStream(zipEtry);
                FileOutputStream outputStream = new FileOutputStream(
                        new File("D:TestZip\\"+zipEtry.getName()));

                byte[] data = new byte[1024];

                while ((inputStream.read(data))!=-1){
                    outputStream.write(data);
                }

                inputStream.close();
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
